﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Discovery;
using System.ServiceModel.Security;
using System.Text;
using VMDS.Services.ClientService;
using VMDS.Services.Contracts;

namespace VMDS.Services.Hosts
{

    public class ClientServiceHost
    {
        private ServiceHost sampleServiceHost;
        public ClientServiceHost(string Address)
        {
            #region binding

            var binding = new NetTcpBinding
            {
                CloseTimeout = TimeSpan.FromMinutes(10),
                SendTimeout = TimeSpan.FromMinutes(10),
                OpenTimeout = TimeSpan.FromMinutes(10),
                ReceiveTimeout = TimeSpan.FromMinutes(10),
                MaxBufferPoolSize = 2147483647, 
                MaxReceivedMessageSize = 2147483647,
                Security =
                {
                    
                    Mode = SecurityMode.Transport,
                    Message = new MessageSecurityOverTcp()
                    {
                        AlgorithmSuite = SecurityAlgorithmSuite.Basic256,
                        ClientCredentialType = MessageCredentialType.Windows
                    },
                    Transport = new TcpTransportSecurity()
                    {
                        ProtectionLevel = ProtectionLevel.EncryptAndSign,
                        ClientCredentialType = TcpClientCredentialType.Windows
                    }
                }


            };

            var bindingmex = new NetTcpBinding
            {
                CloseTimeout = TimeSpan.FromMinutes(10),
                SendTimeout = TimeSpan.FromMinutes(10),
                OpenTimeout = TimeSpan.FromMinutes(10),
                ReceiveTimeout = TimeSpan.FromMinutes(10),
                MaxBufferPoolSize = 1000000,
                MaxReceivedMessageSize = 1000000,
                Security = { Mode = SecurityMode.None }
            };




            #endregion

            #region endpoint adress

            var endpointUri = new Uri(Address);
            var mexEndpointUri = new Uri(String.Format("{0}/mex", endpointUri.ToString()));
            #endregion
            
            sampleServiceHost = new ServiceHost(typeof(ClientService.ClientService), endpointUri);

            sampleServiceHost.Description.Behaviors.Clear();
            var behaviour = new ServiceBehaviorAttribute
            {
                InstanceContextMode = InstanceContextMode.PerCall,
                MaxItemsInObjectGraph=100000,ConcurrencyMode = ConcurrencyMode.Reentrant
            };
            sampleServiceHost.Description.Behaviors.Add(behaviour);

            var serviceMetadataBehavior = new ServiceMetadataBehavior { HttpGetEnabled = false };
            sampleServiceHost.Description.Behaviors.Add(serviceMetadataBehavior);

            var serviceDebugBehavior = new ServiceDebugBehavior
            {
                HttpHelpPageEnabled = true,
                HttpsHelpPageEnabled = true,
                IncludeExceptionDetailInFaults = true
            };
            sampleServiceHost.Description.Behaviors.Add(serviceDebugBehavior);

            /*  var auditBehavior = new ServiceSecurityAuditBehavior()
                                    {
                                        AuditLogLocation = AuditLogLocation.Application,
                                        MessageAuthenticationAuditLevel = AuditLevel.SuccessOrFailure,
                                        ServiceAuthorizationAuditLevel = AuditLevel.SuccessOrFailure,
                                        SuppressAuditFailure = true
                                    };
                sampleServiceHost.Description.Behaviors.Add(auditBehavior);*/


            //  listening end point 

            sampleServiceHost.Credentials.WindowsAuthentication.IncludeWindowsGroups = true;
            sampleServiceHost.AddServiceEndpoint(typeof(IClientServiceContract), binding, endpointUri.ToString());
            
            //  and mex endpoint  
            sampleServiceHost.AddServiceEndpoint(typeof(IMetadataExchange), bindingmex, mexEndpointUri.ToString());
            sampleServiceHost.Authorization.ImpersonateCallerForAllOperations = false;

            sampleServiceHost.Open();
            ClientServiceLogic.Test();
        }
    }

}
